<%
'字符串相关函数
'------------------------------------------------------------------------------

'左补齐字符串
Function LeftPad(byval str, byval length, byval padStr)
    If Len(str) >= length Then
        LeftPad = Left(str, length)
    Else
        LeftPad = String(length - Len(str), padStr) & str
    End If
End Function

'右补齐字符串
Function RightPad(byval str, byval length, byval padStr)
    If Len(str) >= length Then
        RightPad = Left(str, length)
    Else
        RightPad = str & String(length - Len(str), padStr)
    End If
End Function

'截取字符串
Function CutString(byval str, byval length, byval tail)
    If IsNull(str) Or str = "" Then
        CutString = ""
        Exit Function
    End If
    
    If Len(str) > length Then
        CutString = Left(str, length) & tail
    Else
        CutString = str
    End If
End Function

'获取随机字符串
Function RandomString(byval length, byval chars)
    Dim str, i
    If chars = "" Then chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Randomize
    For i = 1 To length
        str = str & Mid(chars, Int(Rnd * Len(chars)) + 1, 1)
    Next
    RandomString = str
End Function

'日期时间相关函数
'------------------------------------------------------------------------------

'格式化日期时间
Function FormatDateTime(byval date_, byval format)
    If IsNull(date_) Or Not IsDate(date_) Then
        FormatDateTime = ""
        Exit Function
    End If
    
    If format = "" Then format = "yyyy-mm-dd hh:ii:ss"
    
    format = Replace(format, "yyyy", Year(date_))
    format = Replace(format, "mm", Right("0" & Month(date_), 2))
    format = Replace(format, "dd", Right("0" & Day(date_), 2))
    format = Replace(format, "hh", Right("0" & Hour(date_), 2))
    format = Replace(format, "ii", Right("0" & Minute(date_), 2))
    format = Replace(format, "ss", Right("0" & Second(date_), 2))
    
    FormatDateTime = format
End Function

'获取时间差
Function DateDiff2(byval startDate, byval endDate, byval type_)
    Dim diff : diff = DateDiff(type_, startDate, endDate)
    Select Case LCase(type_)
        Case "yyyy" DateDiff2 = diff & "年"
        Case "q"    DateDiff2 = diff & "季度"
        Case "m"    DateDiff2 = diff & "月"
        Case "y"    DateDiff2 = diff & "天"
        Case "w"    DateDiff2 = diff & "周"
        Case "d"    DateDiff2 = diff & "天"
        Case "h"    DateDiff2 = diff & "小时"
        Case "n"    DateDiff2 = diff & "分钟"
        Case "s"    DateDiff2 = diff & "秒"
        Case Else   DateDiff2 = diff
    End Select
End Function

'数组相关函数
'------------------------------------------------------------------------------

'判断数组是否包含某个值
Function InArray(byval value, byval arr)
    Dim i
    InArray = False
    If IsArray(arr) Then
        For i = 0 To UBound(arr)
            If CStr(arr(i)) = CStr(value) Then
                InArray = True
                Exit Function
            End If
        Next
    End If
End Function

'数组去重
Function ArrayUnique(byval arr)
    Dim dict : Set dict = Server.CreateObject("Scripting.Dictionary")
    Dim i, item, result()
    
    If Not IsArray(arr) Then
        ArrayUnique = arr
        Exit Function
    End If
    
    For i = 0 To UBound(arr)
        If Not dict.Exists(arr(i)) Then
            dict.Add arr(i), ""
        End If
    Next
    
    ReDim result(dict.Count - 1)
    i = 0
    For Each item In dict.Keys
        result(i) = item
        i = i + 1
    Next
    
    ArrayUnique = result
    Set dict = Nothing
End Function

'数组排序
Function ArraySort(byval arr, byval sortType)
    Dim i, j, temp
    If Not IsArray(arr) Then Exit Function
    
    For i = 0 To UBound(arr) - 1
        For j = i + 1 To UBound(arr)
            If sortType = "ASC" Then
                If arr(i) > arr(j) Then
                    temp = arr(i)
                    arr(i) = arr(j)
                    arr(j) = temp
                End If
            Else
                If arr(i) < arr(j) Then
                    temp = arr(i)
                    arr(i) = arr(j)
                    arr(j) = temp
                End If
            End If
        Next
    Next
    ArraySort = arr
End Function

'URL相关函数
'------------------------------------------------------------------------------

'URL编码
Function UrlEncode2(byval str)
    If IsNull(str) Then
        UrlEncode2 = ""
        Exit Function
    End If
    
    Dim i, c, ret
    For i = 1 To Len(str)
        c = Mid(str, i, 1)
        If c Like "[A-Za-z0-9]" Then
            ret = ret & c
        ElseIf c = " " Then
            ret = ret & "+"
        Else
            ret = ret & "%" & Hex(AscB(c))
        End If
    Next
    UrlEncode2 = ret
End Function

'URL解码
Function UrlDecode2(byval str)
    If IsNull(str) Then
        UrlDecode2 = ""
        Exit Function
    End If
    
    str = Replace(str, "+", " ")
    Dim i, c, ret
    i = 1
    Do While i <= Len(str)
        c = Mid(str, i, 1)
        If c = "%" Then
            ret = ret & Chr(CLng("&H" & Mid(str, i + 1, 2)))
            i = i + 2
        Else
            ret = ret & c
        End If
        i = i + 1
    Loop
    UrlDecode2 = ret
End Function

'安全相关函数
'------------------------------------------------------------------------------

'XSS过滤
Function FilterXSS(byval str)
    If IsNull(str) Then
        FilterXSS = ""
        Exit Function
    End If
    
    str = Replace(str, "&", "&amp;")
    str = Replace(str, "<", "&lt;")
    str = Replace(str, ">", "&gt;")
    str = Replace(str, """", "&quot;")
    str = Replace(str, "'", "&#39;")
    str = Replace(str, vbCrLf, "<br>")
    FilterXSS = str
End Function

'SQL注入过滤
Function FilterSQL(byval str)
    If IsNull(str) Then
        FilterSQL = ""
        Exit Function
    End If
    
    str = Replace(str, "'", "''")
    str = Replace(str, ";", "")
    str = Replace(str, "--", "")
    str = Replace(str, "/*", "")
    str = Replace(str, "*/", "")
    FilterSQL = str
End Function

'文件相关函数
'------------------------------------------------------------------------------

'获取文件扩展名
Function GetFileExt(byval filename)
    Dim pos : pos = InStrRev(filename, ".")
    If pos > 0 Then
        GetFileExt = Mid(filename, pos + 1)
    Else
        GetFileExt = ""
    End If
End Function

'获取文件大小显示
Function GetFileSize(byval size)
    If size < 1024 Then
        GetFileSize = size & "B"
    ElseIf size < 1024 * 1024 Then
        GetFileSize = Round(size / 1024, 2) & "KB"
    ElseIf size < 1024 * 1024 * 1024 Then
        GetFileSize = Round(size / 1024 / 1024, 2) & "MB"
    Else
        GetFileSize = Round(size / 1024 / 1024 / 1024, 2) & "GB"
    End If
End Function

'其他辅助函数
'------------------------------------------------------------------------------

'获取客户端IP
Function GetIP()
    Dim ip : ip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
    If ip = "" Then ip = Request.ServerVariables("REMOTE_ADDR")
    If ip = "" Then ip = Request.ServerVariables("HTTP_CLIENT_IP")
    If ip = "" Then ip = "0.0.0.0"
    GetIP = ip
End Function

'获取客户端浏览器
Function GetBrowser()
    GetBrowser = Request.ServerVariables("HTTP_USER_AGENT")
End Function

'判断是否为移动设备
Function IsMobile()
    Dim agent : agent = LCase(GetBrowser())
    If InStr(agent, "mobile") > 0 Or InStr(agent, "android") > 0 Or _
       InStr(agent, "iphone") > 0 Or InStr(agent, "ipad") > 0 Or _
       InStr(agent, "windows phone") > 0 Then
        IsMobile = True
    Else
        IsMobile = False
    End If
End Function

'判断是否为AJAX请求
Function IsAjax()
    Dim xhr : xhr = Request.ServerVariables("HTTP_X_REQUESTED_WITH")
    If LCase(xhr) = "xmlhttprequest" Then
        IsAjax = True
    Else
        IsAjax = False
    End If
End Function

'判断是否为POST请求
Function IsPost()
    If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
        IsPost = True
    Else
        IsPost = False
    End If
End Function

'获取当前页面URL
Function GetUrl()
    Dim url : url = "http"
    If Request.ServerVariables("HTTPS") = "on" Then url = url & "s"
    url = url & "://" & Request.ServerVariables("HTTP_HOST") & Request.ServerVariables("URL")
    If Request.ServerVariables("QUERY_STRING") <> "" Then
        url = url & "?" & Request.ServerVariables("QUERY_STRING")
    End If
    GetUrl = url
End Function

'IIF函数实现
'------------------------------------------------------------------------------

'IIF条件判断
Function IIF(byval condition, byval trueValue, byval falseValue)
    If condition Then
        IIF = trueValue
    Else
        IIF = falseValue
    End If
End Function

'多重IIF判断
Function IIFMulti(byval conditions, byval values)
    Dim i
    If Not IsArray(conditions) Or Not IsArray(values) Then
        IIFMulti = values
        Exit Function
    End If
    
    For i = 0 To UBound(conditions)
        If conditions(i) Then
            IIFMulti = values(i)
            Exit Function
        End If
    Next
    
    '如果没有匹配的条件，返回最后一个值
    If UBound(values) > UBound(conditions) Then
        IIFMulti = values(UBound(values))
    Else
        IIFMulti = Null
    End If
End Function

'Switch语句实现
Function Switch(byval args)
    If Not IsArray(args) Then Exit Function
    
    Dim i
    For i = 0 To UBound(args) Step 2
        If i + 1 <= UBound(args) Then
            If args(i) Then
                Switch = args(i + 1)
                Exit Function
            End If
        End If
    Next
    Switch = Null
End Function

'判断是否为上传请求
Function IsUploadRequest()
    Dim contentType : contentType = LCase(Request.ServerVariables("CONTENT_TYPE"))
    If InStr(contentType, "multipart/form-data") > 0 Then
        IsUploadRequest = True
    Else
        IsUploadRequest = False
    End If
End Function
%> 